define(function(require, exports, module){

	var LoadingLayer = module.exports = function(){
		this.$el = $("<div class='app-loading z2000'>" +
						"<div class='loadingblock'>"+
							"<div class='loading'>" +
								"<span></span>" +
								"<span></span>" +
								"<span></span>" +
								"<span></span>" +
								"<span></span>" +
							"</div>" +
						"</div>" +
					"</div>");
		this.processing = 0;
		this.timerToHide = null;
	};

	_.extend(LoadingLayer.prototype, Backbone.Events, {
		show:function(){
			if(Nuui.os == "web"){
				$("body").append(this.$el);
			}else{
				Nuui.client.openWaitPanel("");
			}
		},

		hide:function(){
			if(Nuui.os == "web"){
				this.$el.remove();
			}else{
				Nuui.client.hideWaitPanel("");
			}
		},
		
		handleRequestComing:function(settings){
			if(settings && settings.silent)
				return;

			this.processing++;
			this.show();
			clearTimeout(this.timerToHide);
		},

		handleRequestComplete:function(settings){
			if(settings && settings.silent)
				return;

			this.processing--;
			this.timerToHide = setTimeout(_.bind(function(){
				if(this.processing === 0)
					this.hide();
			}, this), 25);
		}
	});
});
